package cn.dankal.flyaudio.adapter;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.pojo.FavoritesShopEntity;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.store.ProductDetailActivity;

public class MyCollectionAdapter extends BaseRecyclerViewAdapter<FavoritesShopEntity> {

    private boolean isPick = false;

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_collection_store;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    class ViewHolder extends BaseRecyclerViewHolder<FavoritesShopEntity> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(FavoritesShopEntity data, int position) {

            ImageView imageView = itemView.findViewById(R.id.iv_pick);
            if (data.isSelect()) {
                imageView.setImageResource(R.mipmap.pic_mine_circle_light);
            } else {
                imageView.setImageResource(R.mipmap.pic_mine_circle);
            }
            imageView.setVisibility(isPick ? View.VISIBLE : View.GONE);
            itemView.findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LiveEventBus.get("MyCollectionDelete", String.class).post(data.getProductUuid());
                }
            });
            itemView.findViewById(R.id.iv_pick).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    LogUtil.e(position+"------"+bg_card_details_1.isSelected());
                    v.setSelected(!v.isSelected());
                    ImageView imageView = itemView.findViewById(R.id.iv_pick);
                    if (v.isSelected()) {
                        data.setSelect(true);
                        imageView.setImageResource(R.mipmap.pic_mine_circle_light);
                    } else {
                        data.setSelect(false);
                        imageView.setImageResource(R.mipmap.pic_mine_circle);
                    }
                    LiveEventBus.get("MyCollection", boolean.class).post(false);
                }
            });
            setImageUrl(R.id.iv_product_img, data.getMainImg());
            setText(R.id.item_tv_name, data.getProductName());
            setText(R.id.item_tv_price, "¥" + data.getShowPrice());
            setVisibility(R.id.tv_invalid, data.getProductDeleted());
            itemView.findViewById(R.id.content).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!data.getProductDeleted()) {
                        Intent intent = new Intent(getContext(), ProductDetailActivity.class);
                        intent.putExtra("uuid", data.getProductUuid());
                        getContext().startActivity(intent);
                        return;
                    }
                    ToastUtils.showShort("商品已失效");
                }
            });
        }
    }

    public void setPickModel() {
        isPick = true;
        notifyDataSetChanged();
    }

    public void closePickModel() {
        isPick = false;
        notifyDataSetChanged();
    }
}
